<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="java,大数据,hive," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="版权声明:作者：虚无境博客园出处：http://www.cnblogs.com/xuwujingCSDN出处：http://blog.csdn.net/qazwsxpcm　　　　个人博客出处：http://www.panchengming.com原创不易，转载请标明出处，谢谢！ 引言在上一篇中大数据学习系列之二 —– HBase环境搭建(单机) 中，成功搭建了Hadoop+HBase的环境，本文">
<meta name="keywords" content="java,大数据,hive">
<meta property="og:type" content="article">
<meta property="og:title" content="大数据学习系列之三 ----- HBase Java Api 图文详解">
<meta property="og:url" content="http://yoursite.com/2017/12/14/pancm59/index.html">
<meta property="og:site_name" content="虚无境的博客">
<meta property="og:description" content="版权声明:作者：虚无境博客园出处：http://www.cnblogs.com/xuwujingCSDN出处：http://blog.csdn.net/qazwsxpcm　　　　个人博客出处：http://www.panchengming.com原创不易，转载请标明出处，谢谢！ 引言在上一篇中大数据学习系列之二 —– HBase环境搭建(单机) 中，成功搭建了Hadoop+HBase的环境，本文">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182048161?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182217011?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182309374?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182323290?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182334479?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182343466?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182356835?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20171214182413393?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:updated_time" content="2017-12-18T12:35:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="大数据学习系列之三 ----- HBase Java Api 图文详解">
<meta name="twitter:description" content="版权声明:作者：虚无境博客园出处：http://www.cnblogs.com/xuwujingCSDN出处：http://blog.csdn.net/qazwsxpcm　　　　个人博客出处：http://www.panchengming.com原创不易，转载请标明出处，谢谢！ 引言在上一篇中大数据学习系列之二 —– HBase环境搭建(单机) 中，成功搭建了Hadoop+HBase的环境，本文">
<meta name="twitter:image" content="http://img.blog.csdn.net/20171214182048161?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/12/14/pancm59/"/>






  <title>大数据学习系列之三 ----- HBase Java Api 图文详解 | 虚无境的博客</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?39c177d10f6e05ddfa113e02139b9c1c";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">虚无境的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/12/14/pancm59/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="虚无境">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/xuwujing.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="虚无境的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">大数据学习系列之三 ----- HBase Java Api 图文详解</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-14T18:26:39+08:00">
                2017-12-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/hive/" itemprop="url" rel="index">
                    <span itemprop="name">hive</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <script src="\assets\js\APlayer.min.js"> </script><p><strong>版权声明:</strong><br>作者：虚无境<br>博客园出处：<a href="http://www.cnblogs.com/xuwujing" target="_blank" rel="external">http://www.cnblogs.com/xuwujing</a><br>CSDN出处：<a href="http://blog.csdn.net/qazwsxpcm" target="_blank" rel="external">http://blog.csdn.net/qazwsxpcm</a>　　　　<br>个人博客出处：<a href="http://www.panchengming.com" target="_blank" rel="external">http://www.panchengming.com</a><br>原创不易，转载请标明出处，谢谢！</p>
<h2 id="引言"><a href="#引言" class="headerlink" title="引言"></a>引言</h2><p>在上一篇中<a href="http://blog.csdn.net/qazwsxpcm/article/details/78760055" target="_blank" rel="external">大数据学习系列之二 —– HBase环境搭建(单机)</a> 中，成功搭建了Hadoop+HBase的环境，本文则主要讲述使用Java 对HBase的一些操作。</p>
<h2 id="一、事前准备"><a href="#一、事前准备" class="headerlink" title="一、事前准备"></a>一、事前准备</h2><h3 id="1-确认hadoop和hbase成功启动"><a href="#1-确认hadoop和hbase成功启动" class="headerlink" title="1.确认hadoop和hbase成功启动"></a>1.确认hadoop和hbase成功启动</h3><p><img src="http://img.blog.csdn.net/20171214182048161?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<h3 id="2-确认防火墙是否关闭"><a href="#2-确认防火墙是否关闭" class="headerlink" title="2.确认防火墙是否关闭"></a>2.确认防火墙是否关闭</h3><h3 id="3-maven所需要的依赖架包"><a href="#3-maven所需要的依赖架包" class="headerlink" title="3.maven所需要的依赖架包"></a>3.maven所需要的依赖架包</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line">&lt;!--hadoop 相关架包 --&gt;</div><div class="line">		&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hadoop-common&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;2.8.2&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line">		&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hadoop-client&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;2.8.2&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line">		&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hadoop-hdfs&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;2.8.2&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line">		&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hadoop-mapreduce-client-core&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;2.8.2&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line">		&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hadoop-yarn-common&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;2.8.2&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line"></div><div class="line">&lt;!--HBase相关jar --&gt;</div><div class="line">		&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hbase&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hbase-hadoop-compat&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;1.3.1&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line">		&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hbase&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hbase-server&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;1.1.2&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line">&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hbase&lt;/groupId&gt;</div><div class="line">			&lt;artifactId&gt;hbase-client&lt;/artifactId&gt;</div><div class="line">			&lt;version&gt;1.1.2&lt;/version&gt;</div><div class="line">		&lt;/dependency&gt;</div><div class="line">&lt;dependency&gt;</div><div class="line">			&lt;groupId&gt;org.apache.hbase&lt;/groupId&gt;</div><div class="line">		    &lt;artifactId&gt;hbase-common&lt;/artifactId&gt;</div><div class="line">	        &lt;version&gt;1.1.2&lt;/version&gt;</div><div class="line">	    &lt;/dependency&gt;</div></pre></td></tr></table></figure>
<h3 id="4-修改hosts文件（可选）"><a href="#4-修改hosts文件（可选）" class="headerlink" title="4.修改hosts文件（可选）"></a>4.修改hosts文件（可选）</h3><p>   修改Windows  C:\Windows\System32\drivers\etc 目录下的hosts文件，添加hbase的主机ip和主机名做关系映射。</p>
<p>192.168.238.128 master</p>
<p><strong>注:如果不使用映射，那么将代码中的主机名改成IP即可。</strong></p>
<h3 id="5-HBase的原理"><a href="#5-HBase的原理" class="headerlink" title="5.HBase的原理"></a>5.HBase的原理</h3><p>这篇文章介绍得很详细:<br><a href="http://blog.csdn.net/woshiwanxin102213/article/details/17584043" target="_blank" rel="external">http://blog.csdn.net/woshiwanxin102213/article/details/17584043</a></p>
<h2 id="二、测试示例"><a href="#二、测试示例" class="headerlink" title="二、测试示例"></a>二、测试示例</h2><h3 id="1-创建表"><a href="#1-创建表" class="headerlink" title="1.创建表"></a>1.创建表</h3><p>创建两张表 t_student、t_student_info 这两张表，并添加两个列族<br>创建成功之后可以在 hbase shell和16010界面中看到。<img src="http://img.blog.csdn.net/20171214182217011?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<p><img src="http://img.blog.csdn.net/20171214182309374?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<p><img src="http://img.blog.csdn.net/20171214182323290?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<h3 id="2-添加数据"><a href="#2-添加数据" class="headerlink" title="2.添加数据"></a>2.添加数据</h3><p>   成功创建表之后，在这两张表中插入数据。<br>   因为HBase是动态数据库，所以列是可以新增的。<br>   HBase的新增和修改是一个方法，数据相同的，后来的数据会将前面的覆盖掉！<br><img src="http://img.blog.csdn.net/20171214182334479?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<p><img src="http://img.blog.csdn.net/20171214182343466?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<h3 id="3-查询数据"><a href="#3-查询数据" class="headerlink" title="3.查询数据"></a>3.查询数据</h3><p>分别根据表名、行健、列族、列来查询<br><img src="http://img.blog.csdn.net/20171214182356835?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<h3 id="4-删除数据"><a href="#4-删除数据" class="headerlink" title="4.删除数据"></a>4.删除数据</h3><p>删除其中的一条数据<br><img src="http://img.blog.csdn.net/20171214182413393?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<h2 id="三、代码示例"><a href="#三、代码示例" class="headerlink" title="三、代码示例"></a>三、代码示例</h2><h3 id="工具类"><a href="#工具类" class="headerlink" title="工具类"></a>工具类</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div><div class="line">312</div><div class="line">313</div><div class="line">314</div><div class="line">315</div><div class="line">316</div><div class="line">317</div><div class="line">318</div><div class="line">319</div><div class="line">320</div><div class="line">321</div><div class="line">322</div><div class="line">323</div><div class="line">324</div><div class="line">325</div><div class="line">326</div><div class="line">327</div><div class="line">328</div><div class="line">329</div><div class="line">330</div><div class="line">331</div><div class="line">332</div><div class="line">333</div><div class="line">334</div><div class="line">335</div><div class="line">336</div><div class="line">337</div><div class="line">338</div><div class="line">339</div><div class="line">340</div><div class="line">341</div><div class="line">342</div><div class="line">343</div><div class="line">344</div><div class="line">345</div><div class="line">346</div><div class="line">347</div><div class="line">348</div><div class="line">349</div><div class="line">350</div><div class="line">351</div><div class="line">352</div><div class="line">353</div><div class="line">354</div><div class="line">355</div><div class="line">356</div><div class="line">357</div><div class="line">358</div><div class="line">359</div><div class="line">360</div><div class="line">361</div><div class="line">362</div><div class="line">363</div><div class="line">364</div><div class="line">365</div><div class="line">366</div><div class="line">367</div><div class="line">368</div><div class="line">369</div><div class="line">370</div><div class="line">371</div><div class="line">372</div><div class="line">373</div><div class="line">374</div><div class="line">375</div><div class="line">376</div><div class="line">377</div><div class="line">378</div><div class="line">379</div><div class="line">380</div><div class="line">381</div><div class="line">382</div><div class="line">383</div><div class="line">384</div><div class="line">385</div><div class="line">386</div><div class="line">387</div><div class="line">388</div><div class="line">389</div><div class="line">390</div><div class="line">391</div><div class="line">392</div><div class="line">393</div></pre></td><td class="code"><pre><div class="line">import java.io.IOException;</div><div class="line">import java.util.ArrayList;</div><div class="line">import java.util.HashMap;</div><div class="line">import java.util.List;</div><div class="line">import java.util.Map;</div><div class="line"></div><div class="line">import org.apache.hadoop.conf.Configuration;</div><div class="line">import org.apache.hadoop.hbase.Cell;</div><div class="line">import org.apache.hadoop.hbase.CellUtil;</div><div class="line">import org.apache.hadoop.hbase.HBaseConfiguration;</div><div class="line">import org.apache.hadoop.hbase.HColumnDescriptor;</div><div class="line">import org.apache.hadoop.hbase.HTableDescriptor;</div><div class="line">import org.apache.hadoop.hbase.TableName;</div><div class="line">import org.apache.hadoop.hbase.client.Admin;</div><div class="line">import org.apache.hadoop.hbase.client.Connection;</div><div class="line">import org.apache.hadoop.hbase.client.ConnectionFactory;</div><div class="line">import org.apache.hadoop.hbase.client.Delete;</div><div class="line">import org.apache.hadoop.hbase.client.Get;</div><div class="line">import org.apache.hadoop.hbase.client.Put;</div><div class="line">import org.apache.hadoop.hbase.client.Result;</div><div class="line">import org.apache.hadoop.hbase.client.ResultScanner;</div><div class="line">import org.apache.hadoop.hbase.client.Scan;</div><div class="line">import org.apache.hadoop.hbase.client.Table;</div><div class="line">import org.apache.hadoop.hbase.util.Bytes;</div><div class="line"></div><div class="line">import com.alibaba.fastjson.JSONObject;</div><div class="line"></div><div class="line">/**</div><div class="line"> * </div><div class="line"> * Title: HBaseUtil</div><div class="line"> * Description: HBase工具类 </div><div class="line"> * Version:1.0.0</div><div class="line"> * @author pancm</div><div class="line"> * @date 2017年12月6日</div><div class="line"> */</div><div class="line">public class HBaseUtil &#123;</div><div class="line">	/** hadoop 连接 */</div><div class="line">	private static Configuration conf = null;</div><div class="line">	/** hbase 连接 */</div><div class="line">	private static Connection con = null;</div><div class="line">	/** 会话 */</div><div class="line">	private static Admin admin = null;</div><div class="line"></div><div class="line">	private static String ip =&quot;master&quot;;</div><div class="line">    private static String port =&quot;2181&quot;;</div><div class="line">    private static String port1 =&quot;9001&quot;;</div><div class="line">	   </div><div class="line">   // 初始化连接</div><div class="line">   static &#123;</div><div class="line">	   // 获得配制文件对象</div><div class="line">       conf = HBaseConfiguration.create(); </div><div class="line">       // 设置配置参数</div><div class="line">		conf.set(&quot;hbase.zookeeper.quorum&quot;, ip);</div><div class="line">		conf.set(&quot;hbase.zookeeper.property.clientPort&quot;, port);  </div><div class="line">		//如果hbase是集群，这个必须加上 </div><div class="line">		//这个ip和端口是在hadoop/mapred-site.xml配置文件配置的</div><div class="line">		conf.set(&quot;hbase.master&quot;, ip+&quot;:&quot;+port1); </div><div class="line">   &#125;</div><div class="line">		</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 获取连接</div><div class="line">	 * </div><div class="line">	 * @return</div><div class="line">	 */</div><div class="line">	public synchronized static Connection getConnection() &#123;</div><div class="line">		try &#123;</div><div class="line">			if (null == con || con.isClosed()) &#123;</div><div class="line">				// 获得连接对象</div><div class="line">				con = ConnectionFactory.createConnection(conf);</div><div class="line">			&#125;</div><div class="line">		&#125; catch (IOException e) &#123;</div><div class="line">			System.out.println(&quot;获取连接失败!&quot;);</div><div class="line">			e.printStackTrace();</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		return con;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 连接关闭</div><div class="line">	 */</div><div class="line">	public static void close() &#123;</div><div class="line">		try &#123;</div><div class="line">			if (admin != null) &#123;</div><div class="line">				admin.close();</div><div class="line">			&#125;</div><div class="line">			if (con != null) &#123;</div><div class="line">				con.close();</div><div class="line">			&#125;</div><div class="line">		&#125; catch (IOException e) &#123;</div><div class="line">			System.out.println(&quot;连接关闭失败！&quot;);</div><div class="line">			e.printStackTrace();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 创建表</div><div class="line">	 * </div><div class="line">	 * @param tableName</div><div class="line">	 *            表名</div><div class="line">	 * @param columnFamily</div><div class="line">	 *            列族</div><div class="line">	 */</div><div class="line">	public static void creatTable(String tableName, String[] columnFamily) &#123;</div><div class="line">		if(null==tableName||tableName.length()==0)&#123;</div><div class="line">			return;</div><div class="line">		&#125;</div><div class="line">		if(null==columnFamily||columnFamily.length==0)&#123;</div><div class="line">			return;</div><div class="line">		&#125;</div><div class="line">		// 创建表名对象</div><div class="line">		TableName tn = TableName.valueOf(tableName);</div><div class="line">		// a.判断数据库是否存在</div><div class="line">		try &#123;</div><div class="line">			// 获取会话</div><div class="line">			admin = getConnection().getAdmin();</div><div class="line">			if (admin.tableExists(tn)) &#123;</div><div class="line">				System.out.println(tableName + &quot; 表存在，删除表....&quot;);</div><div class="line">				// 先使表设置为不可编辑</div><div class="line">				admin.disableTable(tn);</div><div class="line">				// 删除表</div><div class="line">				admin.deleteTable(tn);</div><div class="line">				System.out.println(&quot;表删除成功.....&quot;);</div><div class="line">			&#125;</div><div class="line">			// 创建表结构对象</div><div class="line">			HTableDescriptor htd = new HTableDescriptor(tn);</div><div class="line">			for (String str : columnFamily) &#123;</div><div class="line">				// 创建列族结构对象</div><div class="line">				HColumnDescriptor hcd = new HColumnDescriptor(str);</div><div class="line">				htd.addFamily(hcd);</div><div class="line">			&#125;</div><div class="line">			// 创建表</div><div class="line">			admin.createTable(htd);</div><div class="line">			System.out.println(tableName + &quot; 表创建成功！&quot;);</div><div class="line">		&#125; catch (IOException e) &#123;</div><div class="line">			e.printStackTrace();</div><div class="line">		&#125; finally &#123;</div><div class="line">			close();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 数据单条插入或更新</div><div class="line">	 * </div><div class="line">	 * @param tableName</div><div class="line">	 *            表名</div><div class="line">	 * @param rowKey</div><div class="line">	 *            行健 (主键)</div><div class="line">	 * @param family</div><div class="line">	 *            列族</div><div class="line">	 * @param qualifier</div><div class="line">	 *            列</div><div class="line">	 * @param value</div><div class="line">	 *            存入的值</div><div class="line">	 * @return</div><div class="line">	 */</div><div class="line">	public static void insert(String tableName, String rowKey, String family,</div><div class="line">			String qualifier, String value) &#123;</div><div class="line">		Table t = null;</div><div class="line">		try &#123;</div><div class="line">			t = getConnection().getTable(TableName.valueOf(tableName));</div><div class="line">			Put put = new Put(Bytes.toBytes(rowKey));</div><div class="line">			put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),</div><div class="line">					Bytes.toBytes(value));</div><div class="line">			t.put(put);</div><div class="line">			System.out.println(tableName + &quot; 更新成功!&quot;);</div><div class="line">		&#125; catch (IOException e) &#123;</div><div class="line">			System.out.println(tableName + &quot; 更新失败!&quot;);</div><div class="line">			e.printStackTrace();</div><div class="line">		&#125; finally &#123;</div><div class="line">			close();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 数据批量插入或更新</div><div class="line">	 * </div><div class="line">	 * @param tableName</div><div class="line">	 *            表名</div><div class="line">	 * @param list</div><div class="line">	 *            hbase的数据 </div><div class="line">	 * @return</div><div class="line">	 */</div><div class="line">	public static void insertBatch(String tableName, List&lt;?&gt; list) &#123;</div><div class="line">		if (null == tableName ||tableName.length()==0) &#123;</div><div class="line">			return;</div><div class="line">		&#125;</div><div class="line">		if( null == list || list.size() == 0)&#123;</div><div class="line">			return;</div><div class="line">		&#125;</div><div class="line">		Table t = null;</div><div class="line">		Put put = null;</div><div class="line">		JSONObject json = null;</div><div class="line">		List&lt;Put&gt; puts = new ArrayList&lt;Put&gt;();</div><div class="line">		try &#123;</div><div class="line">			t = getConnection().getTable(TableName.valueOf(tableName));</div><div class="line">			for (int i = 0, j = list.size(); i &lt; j; i++) &#123;</div><div class="line">				json = (JSONObject) list.get(i);</div><div class="line">				put = new Put(Bytes.toBytes(json.getString(&quot;rowKey&quot;)));</div><div class="line">				put.addColumn(Bytes.toBytes(json.getString(&quot;family&quot;)),</div><div class="line">						Bytes.toBytes(json.getString(&quot;qualifier&quot;)),</div><div class="line">						Bytes.toBytes(json.getString(&quot;value&quot;)));</div><div class="line">				puts.add(put);</div><div class="line">			&#125;</div><div class="line">			t.put(puts);</div><div class="line">			System.out.println(tableName + &quot; 更新成功!&quot;);</div><div class="line">		&#125; catch (IOException e) &#123;</div><div class="line">			System.out.println(tableName + &quot; 更新失败!&quot;);</div><div class="line">			e.printStackTrace();</div><div class="line">		&#125; finally &#123;</div><div class="line">			close();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	/**</div><div class="line">	 * 数据删除 </div><div class="line">	 * @param tableName 表名</div><div class="line">	 * @param rowKey	行健</div><div class="line">	 * @return</div><div class="line">	 */</div><div class="line">    public static void delete(String tableName, String rowKey) &#123;</div><div class="line">    	delete(tableName,rowKey,&quot;&quot;,&quot;&quot;);</div><div class="line">    &#125;</div><div class="line">	</div><div class="line">	/**</div><div class="line">	 * 数据删除 </div><div class="line">	 * @param tableName 表名</div><div class="line">	 * @param rowKey	行健</div><div class="line">	 * @param family	列族</div><div class="line">	 * @return</div><div class="line">	 */</div><div class="line">    public static void delete(String tableName, String rowKey, String family) &#123;</div><div class="line">    	delete(tableName,rowKey,family,&quot;&quot;);</div><div class="line">    &#125;</div><div class="line">	</div><div class="line">	/**</div><div class="line">	 * 数据删除 </div><div class="line">	 * @param tableName 表名</div><div class="line">	 * @param rowKey	行健</div><div class="line">	 * @param family	列族</div><div class="line">	 * @param qualifier 列</div><div class="line">	 * @return</div><div class="line">	 */</div><div class="line">    public static void delete(String tableName, String rowKey, String family,</div><div class="line">            String qualifier) &#123;</div><div class="line">    	if (null == tableName ||tableName.length()==0) &#123;</div><div class="line">			return;</div><div class="line">		&#125;</div><div class="line">		if( null == rowKey || rowKey.length() == 0)&#123;</div><div class="line">			return;</div><div class="line">		&#125;</div><div class="line">    	Table t = null;</div><div class="line">        try &#123;</div><div class="line">            t = getConnection().getTable(TableName.valueOf(tableName));</div><div class="line">            Delete del = new Delete(Bytes.toBytes(rowKey));</div><div class="line">            // 如果列族不为空</div><div class="line"> 			if (null != family &amp;&amp; family.length() &gt; 0) &#123;</div><div class="line"> 				// 如果列不为空</div><div class="line"> 				if (null != qualifier &amp;&amp; qualifier.length() &gt; 0) &#123;</div><div class="line"> 					del.addColumn(Bytes.toBytes(family),</div><div class="line"> 							Bytes.toBytes(qualifier));</div><div class="line"> 				&#125; else &#123;</div><div class="line"> 					del.addFamily(Bytes.toBytes(family));</div><div class="line"> 				&#125;</div><div class="line"> 			&#125;      </div><div class="line">            t.delete(del);    </div><div class="line">        &#125; catch (IOException e) &#123;</div><div class="line">        	System.out.println(&quot;删除失败!&quot;);</div><div class="line">            e.printStackTrace();</div><div class="line">        &#125; finally &#123;</div><div class="line">          close();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">	</div><div class="line">	/**</div><div class="line">	 * 查询该表中的所有数据</div><div class="line">	 * </div><div class="line">	 * @param tableName</div><div class="line">	 *            表名</div><div class="line">	 */</div><div class="line">	public static void select(String tableName) &#123;</div><div class="line">		if(null==tableName||tableName.length()==0)&#123;</div><div class="line">			return;</div><div class="line">		&#125;</div><div class="line">		Table t = null;</div><div class="line">		List&lt;Map&lt;String,Object&gt;&gt; list=new ArrayList&lt;Map&lt;String,Object&gt;&gt;();</div><div class="line">		try &#123;</div><div class="line">			t = getConnection().getTable(TableName.valueOf(tableName));</div><div class="line">			// 读取操作</div><div class="line">			Scan scan = new Scan();</div><div class="line">			// 得到扫描的结果集</div><div class="line">			ResultScanner rs = t.getScanner(scan);</div><div class="line">			if (null == rs ) &#123;</div><div class="line">				return;</div><div class="line">			&#125;</div><div class="line">			for (Result result : rs) &#123;</div><div class="line">				// 得到单元格集合</div><div class="line">				List&lt;Cell&gt; cs = result.listCells();</div><div class="line">				if (null == cs || cs.size() == 0) &#123;</div><div class="line">					continue;</div><div class="line">				&#125;</div><div class="line">				for (Cell cell : cs) &#123;</div><div class="line">					Map&lt;String,Object&gt; map=new HashMap&lt;String, Object&gt;();</div><div class="line">					map.put(&quot;rowKey&quot;, Bytes.toString(CellUtil.cloneRow(cell)));// 取行健</div><div class="line">					map.put(&quot;timestamp&quot;, cell.getTimestamp());// 取到时间戳</div><div class="line">					map.put(&quot;family&quot;, Bytes.toString(CellUtil.cloneFamily(cell)));// 取到列族</div><div class="line">					map.put(&quot;qualifier&quot;, Bytes.toString(CellUtil.cloneQualifier(cell)));// 取到列</div><div class="line">					map.put(&quot;value&quot;, Bytes.toString(CellUtil.cloneValue(cell)));// 取到值</div><div class="line">					list.add(map);</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">			System.out.println(&quot;查询的数据:&quot;+list);</div><div class="line">		&#125; catch (IOException e) &#123;</div><div class="line">			System.out.println(&quot;查询失败!&quot;);</div><div class="line">			e.printStackTrace();</div><div class="line">		&#125; finally &#123;</div><div class="line">			close();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 根据表名和行健查询</div><div class="line">	 * @param tableName</div><div class="line">	 * @param rowKey</div><div class="line">	 */</div><div class="line">	public static void select(String tableName, String rowKey) &#123;</div><div class="line">		select(tableName,rowKey,&quot;&quot;,&quot;&quot;);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	/**</div><div class="line">	 * 根据表名、行健和列族查询</div><div class="line">	 * @param tableName</div><div class="line">	 * @param rowKey</div><div class="line">	 * @param family</div><div class="line">	 */</div><div class="line">	public static void select(String tableName, String rowKey, String family) &#123;</div><div class="line">		select(tableName,rowKey,family,&quot;&quot;);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	/**</div><div class="line">	 * 根据条件明细查询</div><div class="line">	 * </div><div class="line">	 * @param tableName</div><div class="line">	 *            表名</div><div class="line">	 * @param rowKey</div><div class="line">	 *            行健 (主键)</div><div class="line">	 * @param family</div><div class="line">	 *            列族</div><div class="line">	 * @param qualifier</div><div class="line">	 *            列</div><div class="line">	 */</div><div class="line">	public static void select(String tableName, String rowKey, String family,</div><div class="line">			String qualifier) &#123;</div><div class="line">		Table t = null;</div><div class="line">		List&lt;Map&lt;String,Object&gt;&gt; list=new ArrayList&lt;Map&lt;String,Object&gt;&gt;();</div><div class="line">		try &#123;</div><div class="line">			t = getConnection().getTable(TableName.valueOf(tableName));</div><div class="line">			// 通过HBase中的 get来进行查询</div><div class="line">			Get get = new Get(Bytes.toBytes(rowKey));</div><div class="line">			// 如果列族不为空</div><div class="line">			if (null != family &amp;&amp; family.length() &gt; 0) &#123;</div><div class="line">				// 如果列不为空</div><div class="line">				if (null != qualifier &amp;&amp; qualifier.length() &gt; 0) &#123;</div><div class="line">					get.addColumn(Bytes.toBytes(family),</div><div class="line">							Bytes.toBytes(qualifier));</div><div class="line">				&#125; else &#123;</div><div class="line">					get.addFamily(Bytes.toBytes(family));</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">			Result r = t.get(get);</div><div class="line">			List&lt;Cell&gt; cs = r.listCells();</div><div class="line">			if (null == cs || cs.size() == 0) &#123;</div><div class="line">				return;</div><div class="line">			&#125;</div><div class="line">			for (Cell cell : cs) &#123;</div><div class="line">				Map&lt;String,Object&gt; map=new HashMap&lt;String, Object&gt;();</div><div class="line">				map.put(&quot;rowKey&quot;, Bytes.toString(CellUtil.cloneRow(cell)));// 取行健</div><div class="line">				map.put(&quot;timestamp&quot;, cell.getTimestamp());// 取到时间戳</div><div class="line">				map.put(&quot;family&quot;, Bytes.toString(CellUtil.cloneFamily(cell)));// 取到列族</div><div class="line">				map.put(&quot;qualifier&quot;, Bytes.toString(CellUtil.cloneQualifier(cell)));// 取到列</div><div class="line">				map.put(&quot;value&quot;, Bytes.toString(CellUtil.cloneValue(cell)));// 取到值</div><div class="line">				list.add(map);</div><div class="line">			&#125;</div><div class="line">			System.out.println(&quot;查询的数据:&quot;+list);</div><div class="line">		&#125; catch (IOException e) &#123;</div><div class="line">			System.out.println(&quot;查询失败!&quot;);</div><div class="line">			e.printStackTrace();</div><div class="line">		&#125; finally &#123;</div><div class="line">			close();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="测试代码"><a href="#测试代码" class="headerlink" title="测试代码"></a>测试代码</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">import java.util.ArrayList;</div><div class="line">import java.util.List;</div><div class="line"></div><div class="line">import com.alibaba.fastjson.JSONObject;</div><div class="line">/**</div><div class="line"> * </div><div class="line">* Title: hbaseTest</div><div class="line">* Description: HBase 相关测试</div><div class="line">* Version:1.0.0  </div><div class="line">* @author pancm</div><div class="line">* @date 2017年11月23日</div><div class="line"> */</div><div class="line">public class hbaseTest &#123;</div><div class="line">	</div><div class="line">	public static void main(String[] args) &#123;</div><div class="line">		test();</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 一些测试</div><div class="line">	 */</div><div class="line">	private static void test() &#123;</div><div class="line">		String tableName1=&quot;t_student&quot;,tableName2=&quot;t_student_info&quot;;</div><div class="line">		String []columnFamily1=&#123;&quot;st1&quot;,&quot;st2&quot;&#125;;</div><div class="line">		String []columnFamily2=&#123;&quot;stf1&quot;,&quot;stf2&quot;&#125;;</div><div class="line">		HBaseUtil.creatTable(tableName1, columnFamily1);</div><div class="line">		HBaseUtil.creatTable(tableName2, columnFamily2);</div><div class="line">		</div><div class="line">		HBaseUtil.insert(tableName1, &quot;1001&quot;, columnFamily1[0], &quot;name&quot;, &quot;zhangsan&quot;);</div><div class="line">		HBaseUtil.insert(tableName1, &quot;1002&quot;, columnFamily1[0], &quot;name&quot;, &quot;lisi&quot;);</div><div class="line">		HBaseUtil.insert(tableName1, &quot;1001&quot;, columnFamily1[1], &quot;age&quot;, &quot;18&quot;);</div><div class="line">		HBaseUtil.insert(tableName1, &quot;1002&quot;, columnFamily1[1], &quot;age&quot;, &quot;20&quot;);</div><div class="line">		</div><div class="line">		HBaseUtil.insert(tableName2, &quot;1001&quot;, columnFamily2[0], &quot;phone&quot;, &quot;123456&quot;);</div><div class="line">		HBaseUtil.insert(tableName2, &quot;1002&quot;, columnFamily2[0], &quot;phone&quot;, &quot;234567&quot;);</div><div class="line">		HBaseUtil.insert(tableName2, &quot;1001&quot;, columnFamily2[1], &quot;mail&quot;, &quot;123@163.com&quot;);</div><div class="line">		HBaseUtil.insert(tableName2, &quot;1002&quot;, columnFamily2[1], &quot;mail&quot;, &quot;234@163.com&quot;);</div><div class="line">		</div><div class="line">		HBaseUtil.select(tableName1); //查询该表所有数据</div><div class="line">		HBaseUtil.select(tableName1, &quot;1001&quot;); //根据表名和行健查询</div><div class="line">		HBaseUtil.select(tableName2, &quot;1002&quot;,columnFamily2[0]); //根据表名、行健和列族查询</div><div class="line">		HBaseUtil.select(tableName2, &quot;1002&quot;,columnFamily2[1],&quot;mail&quot;); //根据表名、行健、列族、和列查询</div><div class="line">		</div><div class="line">		HBaseUtil.select(tableName1, &quot;1002&quot;); //根据表名和行健查询</div><div class="line">		HBaseUtil.delete(tableName1, &quot;1002&quot;, columnFamily1[0]);//删除数据</div><div class="line">		HBaseUtil.select(tableName1, &quot;1002&quot;); //根据表名和行健查询</div><div class="line">		</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>
     
    <div>
	 +
	  
<div style="text-align:center;color: #ccc;font-size:14px;">
------ 本文结束 ------</div>
<br/>
<div style="border: 1px solid black">
<div style="margin-left:10px">
<span style="font-weight:blod">版权声明</span>
<!-- <img src="/images/xuwujing.png" > -->
<br/>
<p style="font-size: 10px;line-height: 30px"><a href="http://www.panchengming.com/" style="color:#258FC6">xuwujing's Notes</a> by <a href="http://www.panchengming.com/" style="color:#258FC6">ChengMing Pan</a> is licensed under a <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/" style="color:#258FC6">Creative Commons BY-NC-ND 4.0 International License</a>.<br/>
由<a href="http://www.panchengming.com/" style="color:#258FC6">虚无境</a>创作并维护的<a href="http://www.panchengming.com/" style="color:#258FC6">xuwujing's Notes</a>博客采用<a href="https://creativecommons.org/licenses/by-nc-nd/4.0/" style="color:#258FC6">创作共用保留署名-非商业-禁止演绎4.0国际许可证</a>。<br/>
本文首发于<a href="http://www.panchengming.com/" style="color:#258FC6">xuwujing's Notes</a> 博客（ <a href="http://www.panchengming.com/" style="color:#258FC6">http://www.panchengming.com/</a> ），版权所有，侵权必究。</p>
</div>
</div>

	
	</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/java/" rel="tag"># java</a>
          
            <a href="/tags/大数据/" rel="tag"># 大数据</a>
          
            <a href="/tags/hive/" rel="tag"># hive</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/12/12/pancm58/" rel="next" title="Hive基础sql语法(转)">
                <i class="fa fa-chevron-left"></i> Hive基础sql语法(转)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/12/15/pancm60/" rel="prev" title="使用VMware安装linux虚拟机以及相关配置">
                使用VMware安装linux虚拟机以及相关配置 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/xuwujing.png"
               alt="虚无境" />
          <p class="site-author-name" itemprop="name">虚无境</p>
           
              <p class="site-description motion-element" itemprop="description">The way of the future!</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">136</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">30</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">59</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/xuwujing" target="_blank" title="github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  github
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://blog.csdn.net/qazwsxpcm?viewmode=list" target="_blank" title="csdn">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  csdn
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://home.cnblogs.com/u/xuwujing/" target="_blank" title="cnblogs">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  cnblogs
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.woainia.site/" title="woainia" target="_blank">woainia</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://cmsblogs.com/" title="chenssy" target="_blank">chenssy</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://italker.imisty.cn" title="xiaowu" target="_blank">xiaowu</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#引言"><span class="nav-number">1.</span> <span class="nav-text">引言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一、事前准备"><span class="nav-number">2.</span> <span class="nav-text">一、事前准备</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-确认hadoop和hbase成功启动"><span class="nav-number">2.1.</span> <span class="nav-text">1.确认hadoop和hbase成功启动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-确认防火墙是否关闭"><span class="nav-number">2.2.</span> <span class="nav-text">2.确认防火墙是否关闭</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-maven所需要的依赖架包"><span class="nav-number">2.3.</span> <span class="nav-text">3.maven所需要的依赖架包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-修改hosts文件（可选）"><span class="nav-number">2.4.</span> <span class="nav-text">4.修改hosts文件（可选）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-HBase的原理"><span class="nav-number">2.5.</span> <span class="nav-text">5.HBase的原理</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、测试示例"><span class="nav-number">3.</span> <span class="nav-text">二、测试示例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-创建表"><span class="nav-number">3.1.</span> <span class="nav-text">1.创建表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-添加数据"><span class="nav-number">3.2.</span> <span class="nav-text">2.添加数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-查询数据"><span class="nav-number">3.3.</span> <span class="nav-text">3.查询数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-删除数据"><span class="nav-number">3.4.</span> <span class="nav-text">4.删除数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、代码示例"><span class="nav-number">4.</span> <span class="nav-text">三、代码示例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#工具类"><span class="nav-number">4.1.</span> <span class="nav-text">工具类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试代码"><span class="nav-number">4.2.</span> <span class="nav-text">测试代码</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">虚无境</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
 | 
本站总访问量<span id="busuanzi_value_site_pv"></span>次
 | 
本站访客数<span id="busuanzi_value_site_uv"></span>人次
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=857896&auto=0&height=66"></iframe>



        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
